package com.mzy.linear_struct.utils;

import java.util.Arrays;
import java.util.function.Consumer;

/**
 * @Author: codermzy
 * @Date: 2024/04/21/9:43
 * @Description:
 */
public class Utils {


    public static void swap(Integer[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


    public static void testSort(Consumer<Integer[]> sort) {

        for (int i = 0; i < 100; i++) {
            Integer[] arr = null;
            Integer[] arr1 = null;
            Integer[] test = null;
            try {
                arr = new Integer[10];
                for (int j = 0; j < arr.length; j++) {
                    arr[j] = (int) (Math.random() * 10 + 1);
                }
                arr1 = Arrays.copyOf(arr, arr.length);
                test = Arrays.copyOf(arr, arr.length);
                sort.accept(arr);

                Arrays.sort(arr1);

                if (!Arrays.equals(arr, arr1)) {
                    System.out.println("Oops !");
                    System.out.println(Arrays.toString(test));
                }
            } catch (Exception e) {
                System.out.println("发生异常");
                System.out.println(Arrays.toString(test));
                throw new RuntimeException(e);
            }
        }


        // System.out.println("排序前：" + Arrays.toString(arr));


        // System.out.println("排序后：" + Arrays.toString(arr));

    }

    private static void isSorted(Integer[] arr) {
        Integer[] copied = Arrays.copyOf(arr, arr.length);
        Arrays.sort(copied);
        boolean correct = true;
        for (int i = 0; i < arr.length; i++) {
            if (!copied[i].equals(arr[i])) {
                correct = false;
                break;
            }
        }

        System.out.println(correct);
    }
}